package com.hyy.taobao.controller;

import com.hyy.taobao.api.IUserService;
import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        设置客户端发送到服务端 的请求的内容类型
        req.setCharacterEncoding("UTF-8");
//        设置服务端发送到客户端 的响应的内容类型
        resp.setContentType("text/html;utf-8");
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        User user = new User(userName, userPwd);
        IUserService iUserService = new UserServiceImpl();
        User user1 = iUserService.userLogin(user);
        if (user1!=null){
            HttpSession session = req.getSession();
            session.setAttribute("myUser",user1);
//            Cookie cookie = new Cookie("cookie",session);

            req.getRequestDispatcher("taobaoIndex.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg","登录失败，用户名或密码错误！");
            req.getRequestDispatcher("Login.jsp").forward(req,resp);
        }
    }
}
